﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using MySql.Data.MySqlClient;
using System.Text;
using System.Windows.Forms;

namespace TJ_MainNameSpace
{
    public partial class AnalogPoint : Form
    {
        public AnalogPoint()
        {
            InitializeComponent();
        }

        //MySqlConnection con = new MySqlConnection(new MySQLConnectionString(TongJi_EMS.Properties.Settings.Default["server"].ToString(), TongJi_EMS.Properties.Settings.Default["database"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqluser"].ToString(), TongJi_EMS.Properties.Settings.Default["mysqlpassword"].ToString(), shujuyuan3.port).AsString);
        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);

        public static string dbname = "";
        public static string dbdescription = "";
        public static string dbarea = "";
        public static string dbaddress = "";
        public static string dbslavername = "";
        public static string dbsunit = "";
        public static string dbtrans = "";
        private bool loading = false;

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBox2.Visible = !this.groupBox2.Visible;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.textBoxunit.Visible = true;
            //this.label14.Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //this.comboBox1.Items.Add (this.textBox9.Text);
            this.textBoxunit.Text = "";
            this.textBoxunit.Visible = false;
            //this.label14.Visible = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            this.comboBox1.Items.Remove(this.comboBox1.Text);

        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (this.comboBox2.Text != "")
            {
                Peizhi3 a = new Peizhi3();
                a.Show();
            }
            else
            {
                MessageBox.Show("请选择一个I/O设备");
            }

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            this.groupBox3.Enabled = !this.groupBox3.Enabled;
            this.groupBox4.Enabled = !this.groupBox4.Enabled;
        }

        private void button12_Click(object sender, EventArgs e)
        {

        }

        private void button6_Click(object sender, EventArgs e)
        {
            For_shebei a = new For_shebei();
            a.Show();
        }

        //确认
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox10.Text == "" || textBox2.Text == "" || comboBox1.Text == "" || comboBox2.Text == "")
                MessageBox.Show("请输入完整的数据");
            else
            {
                try
                {
                    #region 老代码
                    //con.Open();
                    //string sql;
                    //string sqla;
                    //string mysql = "SELECT  id FROM  dbpoint where db_name='" + dbname + "'AND project='" + FormStart.Gongcheng_Name + "'";
                    //MySqlCommand Cmd2 = new MySqlCommand(mysql, con);
                    //object count = Cmd2.ExecuteScalar();
                    //string project = FormStart.Gongcheng_Name;

                    //if (count == null)
                    //    sql = "INSERT INTO dbpoint(db_trans,db_length,db_function,db_name,db_discription,db_area,db_slaverid,db_address,db_type,db_slavername,db_unit,project) VALUES('" + comboBox5.Text + "','" + comboBox3.Text + "','" + comboBox4.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + textBox10.Text + "','" + dbtype + "','" + comboBox2.Text + "','" + textBoxunit.Text + "','" + FormStart.Gongcheng_Name + "')";
                    //else
                    //    sql = "update dbpoint set db_trans='" + comboBox5.Text + "' ,db_length='" + comboBox3.Text + "',db_function='" + comboBox4.Text + "',db_name='" + textBox1.Text + "',db_discription='" + textBox2.Text + "',db_area ='" + comboBox1.Text + "',db_slavername='" + comboBox2.Text + "',db_slaverid ='" + comboBox2.Text + "',db_address='" + textBox10.Text + "',db_unit='" + textBoxunit.Text + "'where db_name='" + dbname + "'AND project='" + FormStart.Gongcheng_Name + "'";

                    //MySqlCommand mInsertCmd2 = new MySqlCommand(sql, con);
                    //mInsertCmd2.ExecuteNonQuery();

                    //if (checkBox1.Checked == true)
                    //{
                    //    string mysql1 = "SELECT  id FROM  alarmlever_a where db_name='" + dbname + "'AND project='" + FormStart.Gongcheng_Name + "'";
                    //    MySqlCommand Cmd21 = new MySqlCommand(mysql1, con);
                    //    object count1 = Cmd21.ExecuteScalar();

                    //    if (count1 == null)
                    //        sqla = "insert into alarmlever_a (db_name,ll_alarmlever,l_alarmlever,h_alarmlever,hh_alarmlever,alarm_elapse,project,l_tips,ll_tips,h_tips,hh_tips) VALUES ('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + FormStart.Gongcheng_Name + "','" + ltextBox.Text + "','" + lltextBox.Text + "','" + htextBox.Text + "','" + hhtextBox.Text + "')";
                    //    else
                    //        sqla = "update alarmlever_a set db_name='" + textBox1.Text + "',ll_alarmlever='" + textBox3.Text + "',l_alarmlever='" + textBox4.Text + "',h_alarmlever='" + textBox5.Text + "',hh_alarmlever='" + textBox6.Text + "',alarm_elapse='" + textBox7.Text + "' ,l_tips='" + ltextBox.Text + "',ll_tips='" + lltextBox.Text + "',h_tips='" + htextBox.Text + "',hh_tips='" + hhtextBox.Text + "'  where db_name='" + dbname + "'AND project='" + FormStart.Gongcheng_Name + "'";

                    //    MySqlCommand mInsertCmda = new MySqlCommand(sqla, con);
                    //    mInsertCmda.ExecuteNonQuery();

                    //    string sqll = "update dbpoint set db_ifalarm='" + 1 + "' where db_name='" + dbname + "'AND project='" + FormStart.Gongcheng_Name + "'";
                    //    MySqlCommand mIn = new MySqlCommand(sqll, con);
                    //    mIn.ExecuteNonQuery();
                    //}
                    //else
                    //{
                    //    string sqll = "update dbpoint set db_ifalarm='" + 0 + "' where db_name='" + dbname + "'AND project='" + FormStart.Gongcheng_Name + "'";

                    //    MySqlCommand mIn = new MySqlCommand(sqll, con);

                    //    mIn.ExecuteNonQuery();
                    //}
                    //con.Close();

                    //dbname = "";
                    //dbdescription = "";
                    //dbarea = "";
                    //dbaddress = "";
                    //dbslavername = "";

                    //this.Close(); 
                    #endregion

                    #region 数据库重新设计后的最新更改
                    con.Open();
                    string sql;
                    string sqla;
                    if (For_shujuku.ifalart)
                    {
                        sql = @"update dbpoint set db_name='" + textBox1.Text + "',db_description = '" + textBox2.Text + "',db_unit = '" + textBoxunit.Text
                            + "',db_devicename = '" + comboBox2.Text + "',db_deviceid = (select id from device where name = '" + comboBox2.Text
                            + "'),db_areaid = (select id from dbarea where area_name = '" + comboBox1.Text + "'),db_address = '" + textBox10.Text + "',db_length = '" + comboBox3.Text
                            + "',db_trans = '" + comboBox5.Text + "',db_functioncode = '" + comboBox4.Text + "' where db_name ='" + dbname + "'";
                        For_shujuku.ifalart = false;
                    }
                    else
                    {
                        sql = @"insert into dbpoint (db_name,db_type,db_description,db_devicename,db_deviceid,db_areaid,db_areaname,db_address,db_functioncode,db_length,db_trans,db_unit,project) values ('"
                            + textBox1.Text + "','" + "Analog" + "','" + textBox2.Text + "','" + comboBox2.Text + "',(select id from device where name ='" + comboBox2.Text + "'),(select id from dbarea where area_name ='" + comboBox1.Text + "'),'"
                            + comboBox1.Text + "','" + textBox10.Text + "','" + comboBox4.Text + "','" + comboBox3.Text + "','" + comboBox5.Text + "','" + textBoxunit.Text + "','" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "')";
                    }
                    MySqlCommand mInsertCmd2 = new MySqlCommand(sql, con);
                    mInsertCmd2.ExecuteNonQuery();
                    //设定报警参数
                    if (checkBox1.Checked == true)
                    {
                        string mysql1 = "SELECT  id FROM  alarmlever_a where db_name='" + textBox1.Text + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
                        MySqlCommand Cmd21 = new MySqlCommand(mysql1, con);
                        object count1 = Cmd21.ExecuteScalar();
                        if (count1 == null)
                            sqla = "insert into alarmlever_a (db_name,ll_alarmlever,l_alarmlever,h_alarmlever,hh_alarmlever,alarm_elapse,project,l_tips,ll_tips,h_tips,hh_tips) VALUES ('" + textBox1.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "','" + ltextBox.Text + "','" + lltextBox.Text + "','" + htextBox.Text + "','" + hhtextBox.Text + "')";
                        else
                            sqla = "update alarmlever_a set db_name='" + textBox1.Text + "',ll_alarmlever='" + textBox3.Text + "',l_alarmlever='" + textBox4.Text + "',h_alarmlever='" + textBox5.Text + "',hh_alarmlever='" + textBox6.Text + "',alarm_elapse='" + textBox7.Text + "' ,l_tips='" + ltextBox.Text + "',ll_tips='" + lltextBox.Text + "',h_tips='" + htextBox.Text + "',hh_tips='" + hhtextBox.Text + "'  where db_name='" + dbname + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
                        MySqlCommand mInsertCmda = new MySqlCommand(sqla, con);
                        mInsertCmda.ExecuteNonQuery();
                        string sqll = "update dbpoint set db_ifalarm='" + 1 + "' where db_name='" + textBox1.Text + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
                        MySqlCommand mIn = new MySqlCommand(sqll, con);
                        mIn.ExecuteNonQuery();
                    }
                    else
                    {
                        string sqll = "update dbpoint set db_ifalarm='" + 0 + "' where db_name='" + textBox1.Text + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

                        MySqlCommand mIn = new MySqlCommand(sqll, con);

                        mIn.ExecuteNonQuery();

                    }
                    con.Close();
                    dbname = "";
                    dbdescription = "";
                    dbarea = "";
                    dbaddress = "";
                    dbslavername = "";
                    this.Close();
                    #endregion
                }
                catch (Exception ex1)
                {
                    MessageBox.Show(ex1.ToString());
                }

            }
        }

        //取消
        private void button4_Click(object sender, EventArgs e)
        {
            dbname = "";
            dbdescription = "";
            dbarea = "";
            dbaddress = "";
            dbslavername = "";
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form6_Load(object sender, EventArgs e)
        {
            loading = true;
            con.Open();
            #region 老代码
            //string sql = "select distinct db_area from dbpoint where project='" + FormStart.Gongcheng_Name + "'";
            //MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);
            //DataSet ds1 = new DataSet();
            //ds1.Clear();
            //oda.Fill(ds1, "dbpoint");
            //this.comboBox1.DataSource = ds1.Tables["dbpoint"];
            //this.comboBox1.DisplayMember = "db_area";
            //this.comboBox1.ValueMember = "db_area";

            //string sqlt = "select name,slaverid from device where project='" + FormStart.Gongcheng_Name + "'";
            //MySqlDataAdapter odat = new MySqlDataAdapter(sqlt, con);
            //DataSet dst = new DataSet();
            //dst.Clear();
            //odat.Fill(dst, "device");
            //this.comboBox2.DataSource = dst.Tables[0];
            //this.comboBox2.DisplayMember = "name";
            //this.comboBox2.ValueMember = "slaverid";
            //this.comboBox2.SelectedIndex = -1; 
            #endregion

            #region 数据库重新设计后的最新更改
            string sql = "select area_name from dbarea where project = '" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
            MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);
            DataSet ds1 = new DataSet();
            ds1.Clear();
            oda.Fill(ds1, "dbarea");
            this.comboBox1.DataSource = ds1.Tables["dbarea"];
            this.comboBox1.DisplayMember = "area_name";
            this.comboBox1.ValueMember = "area_name";

            string sqlt = "select name from device where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";
            MySqlDataAdapter odat = new MySqlDataAdapter(sqlt, con);
            DataSet dst = new DataSet();
            dst.Clear();
            odat.Fill(dst, "device");
            this.comboBox2.DataSource = dst.Tables[0];
            this.comboBox2.DisplayMember = "name";
            this.comboBox2.ValueMember = "name";
            this.comboBox2.SelectedIndex = -1;
            #endregion
            con.Close();
            textBox1.Text = dbname;
            textBox2.Text = dbdescription;
            comboBox1.Text = dbarea;
            textBox10.Text = dbaddress;
            textBoxunit.Text = dbsunit;
            comboBox2.SelectedIndex = comboBox2.FindString(dbslavername, -1);
            comboBox5.SelectedIndex = comboBox5.FindString(dbtrans, -1);
            loading = false;
        }

        private void Form6_FormClosed(object sender, FormClosedEventArgs e)
        {
            dbname = "";
            dbdescription = "";
            dbarea = "";
            dbaddress = "";
            dbslavername = "";
            dbsunit = "";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //数据库重新设计后的最新更改
            if (loading) return;
            con.Open();
            string sql = "select name from device where area_name='" + comboBox1.Text + "'";
            MySqlDataAdapter da = new MySqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds, "device");
            comboBox2.DataSource = ds.Tables["device"];
            comboBox2.DisplayMember = "name";
            comboBox2.ValueMember = "name";
            con.Close();
        }

    }
}